/**
 * node.js的入口文件.
 * 必须的参数：file - 文件名称； callback - jsonp形式调用
 * 返回jsonp形式
 */
var  express = require("express"),
	app = express();
var  http = require('http');
var  url = require('url');
var  querystring = require('querystring');
var  fs = require('fs');
var  path = require('path');
app.use("/", express.static("dist"));
function writeMsg(rep, msg) {
    rep.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8;' ,"Access-Control-Allow-Origin":"*"});
    rep.write(msg);
    rep.end();
}
// 获取JSON文件
app.get("/api",(res, rep)=> {
    //获取url的query
    var arg = url.parse(res.url).query;
    //转换为json形式
    var arg_json = querystring.parse(arg);
    console.log(arg_json);
    //输出的内容
    var content = '';
    var filepath = arg_json.file;
    var callback = arg_json.callback;

    if (filepath) {
        //file -- 本地的json文件路径
        filepath = path.resolve('./json/' + filepath);
        //判断文件是否存在
        if (!fs.existsSync(filepath)) {
            content = 'error:文件不存在';
            writeMsg(rep, content);
        } else {
            fs.readFile(filepath, 'utf-8', function (err, data) {
                if (err) {
                    console.log(err);
                    content = 'error:文件读取失败';
                    writeMsg(rep, content);
                } else {
                    //读取成功
                    if (callback) {
                        //callback -- jsonp形式
                        rep.writeHead(200, { 'Content-Type': 'application/javascript;charset=utf-8;' });
                        content = callback + '(' + data + ');';
                        rep.write(content);
                        rep.end();
                        return false;
                    }
                    else {
                        //callback -- jsonp形式
                        rep.writeHead(200, { 'Content-Type': 'text/json;charset=utf-8;' });
                        rep.write(data);
                        rep.end();
                        return false;
                    }
                }
            });
        }
    } else {
        content = 'error:参数错误';
        writeMsg(rep, content);
    }


})
var nameMap={};
app.get("/api/isAlreay",(res,rep)=>{
        //获取url的query
        var arg = url.parse(res.url).query;
        //转换为json形式
        var param = querystring.parse(arg);
        if(nameMap.hasOwnProperty(param.name)){
            writeMsg(rep,"true")
        }else{
            nameMap[param.name]=param.name;
            writeMsg(rep,"false")
        }

})
var emailMap={};
app.get("/resp.asp",(res,rep)=>{
    //获取url的query
    var arg = url.parse(res.url).query;
    //转换为json形式
    var param = querystring.parse(arg);
    
    if(emailMap.hasOwnProperty(param.email)){
        writeMsg(rep,"false")
    }else{
        writeMsg(rep,"true")
        emailMap[param.email]=param.email;
    }

})


let server = app.listen(3000, function () {
	let host = server.address().address;
	let port = server.address().port;
	console.log("Example app listening at http://%s:%s", host, port);
});